package com.csoa.community.net;

import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.csoa.community.activity.BaseActivity.OnUICallback;
import com.csoa.community.requests.AbstractRequest;
import com.csoa.community.utils.AndroidUtils;
import com.csoa.community.utils.HttpUtils;
import com.csoa.community.utils.StringUtils;

public class WcfAsyncTask extends BaseAsyncTask {

	public WcfAsyncTask(Context cxt, OnUICallback callback) {
		super(cxt, callback);
	}

	@Override
	protected AbstractRequest doInBackground(AbstractRequest... params) {
		AbstractRequest request = (AbstractRequest) params[0];
		try {
			if (!AndroidUtils.isNetworkValidate(activity)) {
				throw new ErrorMsg("network");
			}
			if (request == null && !this.isCancelled())
				this.cancel(true);
			String method = request.getMethod();
			if (method == null && !this.isCancelled())
				this.cancel(true);
			List<BasicNameValuePair> requestValues = request.getRequestValues();
			String url = request.getUrl();
			String soapAction = request.getSoapAciton();
			String result = "";
			if(StringUtils.isEmpty(url) && StringUtils.isEmpty(soapAction)) {
				result = HttpUtils.getInst().getStringByWcf(method, requestValues);
			}else {
				result = HttpUtils.getInst().getStringByWcf(method, requestValues,url,soapAction);
			}
			request.read(result);
		} catch (ErrorMsg e) {
			this.cancel(true);
			if (e.isNetWorkError()) {
				publishProgress(-1, new ErrorMsg("network"));
			} else {
				publishProgress(-1, e.toString());
			}
		}
		return request;
	}
}
